package com.example.reportgenerator.controller;

import com.example.reportgenerator.entity.Work;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/wordDownload")
public class WeekReportDownload {

    @RequestMapping(value = "/load")
    public void generateWord(HttpServletResponse response) throws IOException, XDocReportException {

        InputStream in = this.getClass()
                .getResourceAsStream("/report-template.docx");
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
                in, TemplateEngineKind.Velocity);

        IContext context = report.createContext();
        // 周次
        context.put("num",1);
        // 本周计划
        context.put("summary",Work.summarieArr[0]);
        // 教师评语
        context.put("remark",Work.remarkArr[1]);

        FieldsMetadata metadata = report.createFieldsMetadata();
        metadata.addFieldAsList("work.time");
        metadata.addFieldAsList("work.week");
        metadata.addFieldAsList("work.people");
        metadata.addFieldAsList("work.content");
        report.setFieldsMetadata(metadata);

        Calendar calendar = Calendar.getInstance();
        // 初始时间
        calendar.set(2022,8,29);
        String startTime = this.getDateString(calendar.getTime());
        context.put("startTime",startTime);
        List<Work> works = new ArrayList<>();

        for (int i = 0; i < 6; i++) {
            String time = this.getDateString(calendar.getTime());
            Work work = new Work(time,Work.weekArr[i],Work.contentArr[i]);
            works.add(work);
            // 加一天
            calendar.add(Calendar.DATE,1);
        }
        context.put("work",works);
        String lastTime = this.getDateString(calendar.getTime());
        context.put("lastTime",lastTime);


        OutputStream out = new FileOutputStream(new File(
                "src/main/resources/report.docx"));
        report.process(context, out);


        //浏览器端下载
//        response.setCharacterEncoding("utf-8");
//        response.setContentType("application/msword");
//        String fileName = "report.docx";
//        response.setHeader("Content-Disposition", "attachment;filename="
//                .concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
//        report.process(context, response.getOutputStream());
    }
    // 时间戳转换为字符串
    public String getDateString(Date date){
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = formatter.format(date);
        return dateString;
    }
    // 字符串转换为时间戳
    public Date getDateNum(String str){
        // 注意str格式必须于sdf的格式一直才可转换
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

//    public static void main(String[] args) throws IOException, XDocReportException {
//        new WeekReportDownload().generateWord(null);
//    }

}
